package com.lovo.service.impl;

import com.lovo.bean.CommodityBean;
import com.lovo.bean.CommoditySnapshotBean;
import com.lovo.bean.OrderBean;
import com.lovo.bean.OrderCommodityBean;
import com.lovo.dao.IOrderDAO;
import com.lovo.result.GlobalHandleException;
import com.lovo.result.ResultCode;
import com.lovo.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.util.RedisUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bdk
 * @since 2021-03-10
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class OrderServiceImpl extends ServiceImpl<IOrderDAO, OrderBean> implements IOrderService {
    @Resource
    private ICommodityService commodityService;
    @Resource
    private ICommoditySnapshotService commoditySnapshotService;
    @Resource
    private IOrderDAO orderDAO;
    @Resource
    private IOrderCommodityService orderCommodityService;
    @Resource
    private RedisUtil redisUtil;
    @Override
    public void buy(Long userInfoId, Long commId) throws GlobalHandleException {
        CommodityBean commodityBean = null;
        if(redisUtil.hasKey(commId.toString())){
            commodityBean = (CommodityBean) redisUtil.get(commId.toString());

        }else{
            commodityBean = commodityService.getById(commId);
        }
        if(commodityBean.getCommStock() <= 0){
            throw new GlobalHandleException(ResultCode.COMMODITY_NOT_ENOUGH);
        }
        // 减少库存
        commodityBean.setCommStock(commodityBean.getCommStock() - 1);
        redisUtil.set(commId.toString(),commodityBean);

        CommoditySnapshotBean commoditySnapshotBean = new CommoditySnapshotBean();
        commoditySnapshotBean.setCommId(commodityBean.getCommId());
        commoditySnapshotBean.setCommName(commodityBean.getCommName());
        commoditySnapshotBean.setCommImg(commodityBean.getCommImg());
        commoditySnapshotBean.setCommPrice(commodityBean.getCommPrice());

        commoditySnapshotService.save(commoditySnapshotBean);
        String orderId = redisUtil.generateOrderId();
        OrderBean orderBean = new OrderBean();
        orderBean.setUserId(userInfoId);
        orderBean.setOrderId(orderId);
        orderBean.setCreateTime(LocalDateTime.now());
        orderBean.setStauts(0);
        orderDAO.insert(orderBean);

        OrderCommodityBean orderCommodityBean = new OrderCommodityBean();
        orderCommodityBean.setCommSnapshotId(commoditySnapshotBean.getCommSnapshotId());
        orderCommodityBean.setOrderId(orderId);
        orderCommodityBean.setCount(1);
        orderCommodityService.save(orderCommodityBean);


    }

    @Override
    public void updateStatus(String orderId,int status) {
        OrderBean orderBean = orderDAO.selectById(orderId);
        orderBean.setStauts(status);
        orderDAO.updateById(orderBean);
    }
}
